package com.wangmeng.book.utils;

import java.io.Serializable;
import java.security.MessageDigest;

/**
 * User: 王猛
 * Date: 13-10-7
 */
public class MessageDigestHelper implements Serializable {

    public static String digestBySHA(String message) {
        StringBuffer data = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] mdData = md.digest(message.getBytes());
            for (byte code : mdData) {
                if (code >= 0) {
                    if (code < 16) {
                        data.append("0");
                    }
                    data.append(Integer.toHexString(code & 0xFF));
                } else {
                    data.append(Integer.toHexString((code + 256) & 0xFF));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data.toString();
    }
}
